package com.rc.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.rc.activity.dao.tbl_Profile;
import com.rc.activity.dao.tbl_User;
import com.rc.db.DatabaseSupport;
import com.rc.model.Model;
import com.rc.model.TaskType;
import com.rc.util.Util;
import com.telpoo.frame.model.TaskParams;
import com.telpoo.frame.ui.BaseActivity;

public class FeedBack extends BaseActivity implements OnClickListener {

	/**
	 * call Intent
	 */

	public static void callIntent(Activity ct) {
		Intent it = new Intent(ct, FeedBack.class);
		ct.startActivity(it);
	}

	private Spinner spCategory;
	private String subject = "";
	private TextView txtMessage;
	private LinearLayout llSave, llCancel, llProgress, llContent;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.main_feed_back);
		llSave = (LinearLayout) findViewById(R.id.llSave);
		llCancel = (LinearLayout) findViewById(R.id.llCancel);
		llContent = (LinearLayout) findViewById(R.id.llContent);
		llProgress = (LinearLayout) findViewById(R.id.llProgress);
		txtMessage = (TextView) findViewById(R.id.txtMessage);

		ArrayList<String> arrString = new ArrayList<String>();
		arrString.add(getString(R.string.bug));
		arrString.add(getString(R.string.suggestion));
		spCategory = (Spinner) findViewById(R.id.spCategory);
		ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
				android.R.layout.simple_spinner_dropdown_item, arrString);
		spCategory.setAdapter(spinnerArrayAdapter);

		llSave.setOnClickListener(this);
		llCancel.setOnClickListener(this);

	}

	/**
	 * call sevirce feedback
	 */
	private void callAsynCallServiceReceipt(String fullName, String email,
			String message, String subject) {
		Model mModel = new Model(model, TaskType.TASK_FEED_BACK, null, this);
		subject = spCategory.getSelectedItem().toString();
		if (fullName.indexOf(" ") >= 0)
			fullName = fullName.replace(" ", "+");
		if (email.indexOf(" ") >= 0)
			email = email.replace(" ", "+");
		if (message.indexOf(" ") >= 0)
			message = message.replace(" ", "+");
		if (subject.indexOf(" ") >= 0)
			subject = subject.replace(" ", "+");
		String[] mParams = { fullName, email, message, subject };
		TaskParams params = new TaskParams(mParams);
		if (Util.isNetworkActivated(FeedBack.this)) {
			llProgress.setVisibility(View.VISIBLE);
			llContent.setVisibility(View.GONE);
			model.exeTask(params, mModel);
		} else {
			showToast(getString(R.string.no_connection));
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.llCancel:
			finish();
			break;
		case R.id.llSave:
			tbl_User objUser = new tbl_User();
			objUser = DatabaseSupport.getTbl_User(FeedBack.this);
			tbl_Profile objProfile = new tbl_Profile();
			objProfile = DatabaseSupport.getTbl_Profile(FeedBack.this);
			callAsynCallServiceReceipt(objProfile.getFirstName() + "+"
					+ objProfile.getLastName(), objUser.getEmail(), txtMessage
					.getText().toString(), subject);
			break;
		default:
			break;
		}
	}

	@Override
	public void onSuccess(int taskType, ArrayList<?> list, String msg) {
		// TODO Auto-generated method stub
		super.onSuccess(taskType, list, msg);
		switch (taskType) {
		case TaskType.TASK_FEED_BACK:
			showToast(getString(R.string.send_feed_back_successfull));
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void onFail(int taskType, String msg) {
		// TODO Auto-generated method stub
		super.onFail(taskType, msg);
		showToast(getString(R.string.send_feed_back_fail));
		llProgress.setVisibility(View.GONE);
		llContent.setVisibility(View.VISIBLE);
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}

}
